const express = require('express')
const bodyParser = require('body-parser')
const webpack = require('webpack')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const WebpackConfig = require('./webpack.config')
const cookieParser = require('cookie-parser')
const path = require('path')
const atob = require('atob')
const multipart = require('connect-multiparty')

require('./server2')

const app = express()
const compiler = webpack(WebpackConfig)

app.use(webpackDevMiddleware(compiler, {
  publicPath: '/__build__/',
  stats: {
    colors: true,
    chunks: false
  }
}))

app.use(express.static(__dirname, {
  setHeaders (res) {
    res.cookie('XSRF-TOKEN-D', 'server-token-1234abc')
  }
}))

var allowCrossDomain = function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Credentials', true)
  res.header('Access-Control-Allow-Methods', '*')
  res.header('Access-Control-Allow-Headers', '*')
  next()
}
app.use(allowCrossDomain)

app.use(webpackHotMiddleware(compiler))

app.use(express.static(__dirname))

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser())

const router = express.Router()

router.get('/simple/get', function(req, res) {
  res.json({
    msg: `hello world`
  })
})

router.get('/base/get', function(req, res) {
  res.json(req.query)
})

router.post('/base/post', function(req, res) {
  res.json(req.body)
})

router.post('/base/buffer', function(req, res) {
  let msg = []
  req.on('data', (chunk) => {
    if (chunk) {
      msg.push(chunk)
    }
  })
  req.on('end', () => {
    let buf = Buffer.concat(msg)
    res.json(buf.toJSON())
  })
})

router.post('/base/formData', function(req, res) {
  res.json({
    a: req.body
  })
})

// 报错-随机500
router.get('/error/get', function(req, res) {
  if (Math.random() > 0.5) {
    res.json({
      msg: `hello world`
    })
  } else {
    res.status(500)
    res.end()
  }
})

// 延时报错
router.get('/error/timeout', function(req, res) {
  setTimeout(() => {
    res.json({
      msg: `hello world`
    })
  }, 3000)
})

// extend
router.post('/extend/post', function(req, res) {
  res.json({
    url: '/extend/post',
    msg: req.body
  })
})
router.get('/extend/get', function(req, res) {
  res.json({
    url: '/extend/get'
  })
})
router.put('/extend/put', function(req, res) {
  res.json({
    url: '/extend/put'
  })
})
router.head('/extend/head', function(req, res) {
  res.json({
    url: '/extend/head'
  })
})
router.delete('/extend/delete', function(req, res) {
  res.json({
    url: '/extend/delete'
  })
})
router.options('/extend/options', function(req, res) {
  res.json({
    url: '/extend/options'
  })
})

router.get('/interceptor/get', function(req, res) {
  res.end('hello')
})

// config
router.post('/config/post', function(req, res) {
  res.json(req.body)
})

// cancel
router.get('/cancel/get', function(req, res) {
  setTimeout(() => {
    res.json('hello')
  }, 1000)
})

router.post('/cancel/post', function(req, res) {
  setTimeout(() => {
    res.json(req.body)
  }, 1000)
})

router.get('/more/get', function(req, res) {
  // console.log(req.cookies)
  res.json(req.cookies)
})
registerMoreRouter()
function registerMoreRouter () {

  router.post('/more/upload', function(req, res) {
    console.log(req.body, req.files)
    res.end('upload success!')
  })

  router.post('/more/post', function(req, res) {
    const auth = req.headers.authorization
    const [type, credentials] = auth.split(' ')
    console.log(atob(credentials))
    const [username, password] = atob(credentials).split(':')
    if (type === 'Basic' && username === 'Yee' && password === '123456') {
      res.json(req.body)
    } else {
      res.status(401)
      res.end('UnAuthorization')
    }
  })

  router.get('/more/304', function(req, res) {
    res.status(304)
    res.end()
  })

  router.get('/more/A', function(req, res) {
    res.end('A')
  })

  router.get('/more/B', function(req, res) {
    res.end('B')
  })
}

app.use(router)

const port = process.env.PORT || 8080
module.exports = app.listen(port, () => {
  console.log(`Server listening on http://localhost:${port}, Ctrl+C to stop`)
})
